package cn.kingyang.yiyi.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;

import com.jtech.listener.OnItemClickListener;
import com.jtech.view.JRecyclerView;
import com.jtech.view.RecyclerHolder;
import com.jtech.view.RefreshLayout;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerClickListener;
import com.youth.banner.loader.ImageLoaderInterface;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.kingyang.yiyi.R;
import cn.kingyang.yiyi.mvp.model.bean.HomeInfo;
import cn.kingyang.yiyi.mvp.model.bean.HttpData;
import cn.kingyang.yiyi.mvp.presenter.HomePresenter;
import cn.kingyang.yiyi.mvp.view.HomeView;
import cn.kingyang.yiyi.ui.activity.VideoActivity;
import cn.kingyang.yiyi.ui.adapter.VideoAdapter;
import cn.kingyang.yiyi.ui.fragment.base.BaseMainFragment;
import cn.kingyang.yiyi.utils.AppUtil;


/**
 * Created by Administrator on 2017/2/28.
 */

public class HomeFragment extends BaseMainFragment<HomeView,HomePresenter> implements HomeView, RefreshLayout.OnRefreshListener, OnItemClickListener {

    @BindView(R.id.home_video_jr)
    JRecyclerView mActionJRV;//
    private VideoAdapter mVideoAdapter;//
    @BindView(R.id.refreshlayout)
    RefreshLayout refreshlayout;//下拉刷新组件
    @BindView(R.id.home_banner)
    Banner mBannr;
    public static HomeFragment newInstance() {
        return new HomeFragment();
    }
    @Override
    protected HomePresenter getPresenter() {
        return new HomePresenter();
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView(View view) {

        mVideoAdapter =new VideoAdapter(_mActivity);
        mActionJRV.setLayoutManager(new GridLayoutManager(getActivity(),2));
        mActionJRV.setAdapter(mVideoAdapter);
        mActionJRV.setOnItemClickListener(this);
        refreshlayout.setOnRefreshListener(this);
        refreshlayout.startRefreshing();

        //设置banner样式
        mBannr.setBannerStyle(BannerConfig.NUM_INDICATOR_TITLE);
        //设置图片加载器
        mBannr.setImageLoader(new ImageLoaderInterface(){

            @Override
            public void displayImage(Context context, Object path, View imageView) {
                AppUtil.initImage(context,imageView, (String) path);
            }

            @Override
            public View createImageView(Context context) {
                return null;
            }
        });

        //设置banner动画效果
        mBannr.setBannerAnimation(Transformer.DepthPage);
        //设置自动轮播，默认为true
        mBannr.isAutoPlay(true);
        //设置轮播时间
        mBannr.setDelayTime(3000);
        //设置指示器位置（当banner模式中有指示器时）
        mBannr.setIndicatorGravity(BannerConfig.CENTER);


//        mPresenter.getHomeAction();
    }

    @Override
    public void onStart() {
        super.onStart();
        //开始轮播
        mBannr.startAutoPlay();
    }
    public void update(){
        refreshlayout.startRefreshing();
    }
    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void onError(Throwable t) {

    }


    @Override
    public void onRefresh() {
        mPresenter.getHomeAction();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onStop() {
        super.onStop();
        mBannr.stopAutoPlay();
    }


    @Override
    public void onItemClick(RecyclerHolder recyclerHolder, View view, int i) {
        Intent intent=new Intent(_mActivity, VideoActivity.class);
        intent.putExtra("title",mVideoAdapter.getRealDatas().get(i).getTitle());
        intent.putExtra("url",mVideoAdapter.getRealDatas().get(i).getUrl());
        startActivityForResult(intent,100);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 100:
                //弹出popupwindow
                AppUtil.showPopupWindow(_mActivity,getView());
                break;
        }
    }

    @Override
    public void doHomeInfo(final HttpData<HomeInfo> data) {
        mVideoAdapter.setDatas(data.getData().getTrial());
        List<String> images=new ArrayList<>();
        final List<String> titles=new ArrayList<>();
        for (HomeInfo.BannerBean a:data.getData().getBanner()) {
            images.add(a.getThumb());
            titles.add(a.getTitle());
        }
        //设置图片集合
        mBannr.setImages(images);
        //设置标题集合（当banner样式有显示title时）
        mBannr.setBannerTitles(titles);
        //点击回调
        mBannr.setOnBannerClickListener(new OnBannerClickListener() {
            @Override
            public void OnBannerClick(int position) {
                Intent intent=new Intent(_mActivity, VideoActivity.class);
                intent.putExtra("title",titles.get(position-1));
                intent.putExtra("url",data.getData().getBanner().get(position-1).getUrl());
                startActivityForResult(intent,100);
            }
        });
        //banner设置方法全部调用完毕时最后调用
        mBannr.start();
        refreshlayout.refreshingComplete();
    }
}
